package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.lifecycle.Observer
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.dialog.DialogSelectGenderOrStatus
import com.horoscope.astrology.zodiac.signs.free.view.DatePicker
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.activity_relation_compatibility.*


class RelationCompatibilityActivity : BaseActivity(), View.OnClickListener {

    private lateinit var mGenderOrStatus: DialogSelectGenderOrStatus
    private var genderF = 0
    private var flag = 1

    private var mUserBean = UserBean()

    private var user1 = UserBean()
    private var user2 = UserBean()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_relation_compatibility)
        intent?.let {
            if (it.getBooleanExtra("Alarm", false)) {
                activityShowAd("Push_Name")
            }
        }
        initTitleBar(
            this@RelationCompatibilityActivity,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            getString(R.string.astrology_friendly_relationship)
        )
        mUserBean = AppSettingManager.readObject("user_info") ?: UserBean()
        setListener()
        initData()
        FlurryEventUtil.putDataToEvent(EventType.TYPE_RELATION_COMPATIBILITY_SHOW, null, null)
    }

    private fun initData() {
        mGenderOrStatus = DialogSelectGenderOrStatus(1, "RelationCompatibilityActivity")
        edit_name.setText(mUserBean.name)
        tv_gender_1.text = mUserBean.gender
        tv_birthday.text = mUserBean.birthday
        user1.name = mUserBean.name
        user1.gender = mUserBean.gender
        user1.birthday = mUserBean.birthday
    }

    private fun setListener() {
        tv_birthday.setOnClickListener(this)
        tv_birthday2.setOnClickListener(this)
        tv_gender_1.setOnClickListener(this)
        tv_gender_2.setOnClickListener(this)
        tv_result_relation.setOnClickListener(this)

        edit_name.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                buttonIsEnable()
            }
        })
        edit_name2.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun afterTextChanged(s: Editable?) {
                buttonIsEnable()
            }
        })
    }

    override fun onClick(v: View?) {
        if (!SysUtils.isFastClick()) {
            when (v?.id) {
                R.id.tv_birthday -> {
                    flag = 1
                    showDatePick(0)
                }
                R.id.tv_birthday2 -> {
                    flag = 2
                    showDatePick(1)
                }
                R.id.tv_gender_1 -> {
                    genderF = 1
                    dialogShow(1)

                }
                R.id.tv_gender_2 -> {
                    genderF = 2
                    dialogShow(2)
                }
                R.id.tv_result_relation -> {
                    if (checkText()) {
                        FlurryEventUtil.putDataToEvent(
                            EventType.TYPE_RELATION_COMPATIBILITY_CONTINUE,
                            null,
                            null
                        )
                        try {
                            if (intent.getBooleanExtra("Alarm", false)) {   // 通过通知进入
                                startActivity(
                                    RelationResultActivity.generateIntent(
                                        this,
                                        user1,
                                        user2,
                                        intent.getBooleanExtra("Alarm", false),
                                        intent.getStringExtra("workName")
                                    )
                                )
                            } else {    // 正常进入
                                startActivity(
                                    RelationResultActivity.generateIntent(
                                        this,
                                        user1,
                                        user2
                                    )
                                )
                            }
                        } catch (e: Exception) {
                            startActivity(RelationResultActivity.generateIntent(this, user1, user2))
                        }

                    }
                }

            }
        }
    }

    // 填充数据检查
    private fun checkText(): Boolean {
        val name1 = edit_name.text.toString().trim().replace("\n", "")
        val name2 = edit_name2.text.toString().trim().replace("\n", "")

        if (name1.isNotEmpty() && name2.isNotEmpty()
            && user1.birthday.isNotEmpty()
            && user2.birthday.isNotEmpty()
            && user1.gender.isNotEmpty()
            && user2.gender.isNotEmpty()
        ) {
            user1.name = name1
            user2.name = name2
            return true
        }
        return false
    }

    private fun showDatePick(flag: Int) {
        val datePicker = DatePicker(this@RelationCompatibilityActivity)
        datePicker.ShowDatePick(flag, "RelationCompatibilityActivity")
    }

    private fun dialogShow(tag: Int) {
        mGenderOrStatus.show(supportFragmentManager, tag.toString())
    }

    companion object {
        fun generateIntent(context: Context): Intent {
            return Intent(context, RelationCompatibilityActivity::class.java)
        }
    }

    override fun onResume() {
        super.onResume()
        LiveEventBus.get("DoneRelationCompatibilityActivity", String::class.java)
            .observeSticky(this, Observer<String?> { t ->
                t?.let {
                    mGenderOrStatus.dismiss()
                    if (genderF == 1) {
                        user1.gender = t
                        tv_gender_1.text = t
                    } else {
                        user2.gender = t
                        tv_gender_2.text = t
                    }

                    buttonIsEnable()
                }
            })

        LiveEventBus.get("AlertDialogDateRelationCompatibilityActivity", Bundle::class.java)
            .observeSticky(this, Observer<Bundle?> { t ->
                t?.let {
                    val year = t.getInt("year")
                    val month = t.getInt("moth")
                    val day = t.getInt("day")
                    var mDay = day.toString()
                    var mMonth = month.toString()
                    if (day < 10) {
                        mDay = "0$day"
                    }
                    if (mMonth.toInt() < 10) {
                        mMonth = "0$month"
                    }

                    val str = "$mMonth-$mDay-$year"
                    if (flag == 1) {
                        tv_birthday.text = str
                        user1.birthday = str
                    } else {
                        tv_birthday2.text = str
                        user2.birthday = str
                    }

                    buttonIsEnable()
                }
            })

        buttonIsEnable()
    }


    private fun buttonIsEnable() {
        if (edit_name.text.isEmpty()
            || edit_name2.text.isEmpty()
            || tv_birthday.text.isEmpty()
            || tv_birthday2.text.isEmpty()
            || tv_gender_1.text.isEmpty()
            || tv_gender_2.text.isEmpty()
        ) {
            tv_result_relation.isClickable = false
            tv_result_relation.alpha = 0.5f

        } else {
            tv_result_relation.isClickable = true
            tv_result_relation.alpha = 1f
        }
    }
}
